<?php
require_once 'lib/controllerWithoutGetOrPost.php';
require_once 'lib/db/FuneralHome.php';
$funeralHomeDB = new FuneralHomeDB($db);
require_once 'models/EditFuneralHomeModel.php';
$model = new EditFuneralHomeModel($funeralHomeDB);
require_once 'header2.php';
// Database method
/**
 * setFuneralHomeDetails
 *
 * @return bool
 * @param mixed $regionId
 * @param mixed $cityTownId
 * @param mixed $countryId
 * @param mixed $blurb
 * @param mixed $fax
 * @param mixed $telephoneDD
 * @param mixed $telephone
 * @param mixed $suburb
 * @param mixed $address2
 * @param mixed $address1
 * @param mixed $contactFirstName
 * @param mixed $contactLastName
 * @param mixed $email
 * @param mixed $web
 * @param mixed $id
 */
function setFuneralHomeDetails($regionId, $cityTownId, $countryId, $blurb, $fax, $telephoneDD, $telephone, $suburb, $address2, $address1, $contactFirstName, $contactLastName, $email, $addListingNotify, $web, $id)
{
    global $db;
    $sql = <<< EOT
        UPDATE
            FuneralDetails
        SET
            RegionID = %d,
            CityTownID = %d,
            CountryID = %d,
            Blurb = %d,
            Fax = '%s',
            TelephoneDD = '%s',
            Telephone = '%s',
            Suburb = '%s',
            Address2 = '%s',
            Address1 = '%s',
            ContactFirstName = '%s',
            ContactLastName = '%s',
            Email = '%s',
            AddListingNotify = '%s',
            Web = '%s'
        WHERE
            ID = %d

EOT;
    $sql = sprintf($sql,
        intval($regionId),
        intval($cityTownId),
        intval($countryId),
        $db->escape($blurb),
        $db->escape($fax),
        $db->escape($telephoneDD),
        $db->escape($telephone),
        $db->escape($suburb),
        $db->escape($address2),
        $db->escape($address1),
        $db->escape($contactFirstName),
        $db->escape($contactLastName),
        $db->escape($email),
        $db->escape($addListingNotify),
        $db->escape($web),
        intval($id)
    );
    $success = $db->query($sql);
    return $success === TRUE;
}
// Database method
/**
 * Get details for the funeral home
 *
 * @return array[string]string
 * @param mixed $id
 */
function getFuneralHomeDetails($id)
{
    global $db;
    $id = $db->escape($id);
    $sql = <<< EOT
    SELECT
        FuneralDetails.ID as FuneralDetailsID,
        FuneralHomeID, ContactFirstName, ContactLastName,
        TelephoneDD, Telephone, Address1, Address2, Suburb,
        Fax, Email, AddListingNotify, Web, Blurb, CountryID, RegionID, CityTownID,
        FuneralHome.ID as FuneralHomeID,
        FuneralHome.Name as FuneralHomeName
    FROM
        FuneralDetails
            LEFT JOIN FuneralHome
                ON FuneralDetails.FuneralHomeID = FuneralHome.ID
    WHERE
        FuneralDetails.ID = '$id'
    ORDER BY
        FuneralHome.Name ASC

EOT;
    $result = $db->query($sql);
    $row = mysql_fetch_assoc($result);
    return $row;
}
if ( isset ($_POST['id']) && isset ($_POST['funeralid'])) {
    // Model method
    $hasUpdatedName = FALSE;
    $duplicateName = $model->checkDuplicateFuneralHomes($_POST['funeralname'], $_POST['funeralid']);
    if (!$duplicateName) {
        $hasUpdatedName = $funeralHomeDB->setName($_POST['funeralname'], $_POST['funeralid']);
    }
    // view methods
    echo '<div><a href="funeralhomelist.php">Back to list</a></div>';
    if (!$hasUpdatedName) {
        echo '<div>Could not update funeral home name. The name already exists.</div>';
    }
    $success = setFuneralHomeDetails($_POST['regionid'], $_POST['citytownid'], $_POST['countryid'], $_POST['blurb'], $_POST['fax'], $_POST['telephonedd'], $_POST['telephone'], $_POST['suburb'], $_POST['address2'], $_POST['address1'], $_POST['contactfirstname'], $_POST['contactlastname'], $_POST['email'], $_POST['addlistingnotify'], $_POST['web'], $_GET['id']);
} else {
    // Model code
    $row = getFuneralHomeDetails($_GET['id']);
    $funeralHomeId = $db->get($row['FuneralHomeID']);
    $funeralHomeName = $funeralHomeDB->getName($funeralHomeId);
    $id = $html->text($_GET['id']);
    $funeralHomeId = $html->text($funeralHomeId);
    $email = $html->text($row['Email']);
    $addListingNotifyChecked = '';
    if (!empty($row['AddListingNotify'])) {
        $addListingNotifyChecked = ' checked="checked"';
    }
    $web = $html->text($row['Web']);
    $contactFirstName = $html->text($row['ContactFirstName']);
    $contactLastName = $html->text($row['ContactLastName']);
    $address1 = $html->text($row['Address1']);
    $address2 = $html->text($row['Address2']);
    $suburb = $html->text($row['Suburb']);
    $telephone = $row['Telephone'];
    $telephoneDD = $row['TelephoneDD'];
    $telephone = $row['Telephone'];
    $fax = $row['Fax'];
    $blurb = $row['Blurb'];
    // Model code for cityTownOptions
    $quer = "SELECT ID, Name FROM CityTown";
    $be = mysql_query($quer, $link);
    $cityTownOptions = '<option value="">No city/town set</option>';
    while ($row123 = mysql_fetch_assoc($be)) {
        if ($row123['ID'] == $row['CityTownID']) {
            $cityTownOptions .= '<option selected="selected" value="'.$row123['ID'].'">'.$row123['Name'].'</option>';
        } else {
            $cityTownOptions .= '<option value="'.$row123['ID'].'">'.$row123['Name'].'</option>';
        }
    }
    // Model code for regionOptions
    $quer = "SELECT ID, Name from Region";
    $be = mysql_query($quer, $link);
    $regionOptions = '<option value="">No region set</option>';
    while ($row123 = mysql_fetch_assoc($be)) {
        if ($row123['ID'] == $row['RegionID']) {
            $regionOptions .= '<option selected="selected" value="'.$row123['ID'].'">'.$row123['Name'].'</option>';
        } else {
            $regionOptions .= '<option value="'.$row123['ID'].'">'.$row123['Name'].'</option>';
        }
    }
    // Model code for countryOptions
    $quer = "SELECT ID, Name from Country";
    $be = mysql_query($quer, $link);
    $countryOptions = '<option value="">No country set</option>';
    while ($row123 = mysql_fetch_assoc($be)) {
        if ($row123['ID'] == $row['CountryID']) {
            $countryOptions .= '<option selected="selected" value="'.$row123['ID'].'">'.$row123['Name'].'</option>';
        } else {
            $countryOptions .= '<option value=""'.$row123['ID'].'">'.$row123['Name'].'</option>';
        }
    }
    // View code
    echo <<< EOT
    <form action="editfuneralhome.php?id=$id" method="post">
	    <input name="id" type="hidden" value="$id" />
        <input name="funeralid" type="hidden" value="$funeralHomeId" />
        <table>
            <tr><td>Name</td><td><input type="text" name="funeralname" value="$funeralHomeName" /></td></tr>
            <tr><td>Email</td><td><input type="text" name="email" value="$email" /></td></tr>
            <tr><td>&nbsp;</td><td><input type="checkbox" id="addlistingnotify" name="addlistingnotify" value="1"$addListingNotifyChecked /> <label for="addlistingnotify">Notify when listing added</td></tr>
            <tr><td>Web</td><td><input type="text" name="web" value="$web" /></td></tr>
            <tr><td>Contact First Name</td><td><input type="text" name="contactfirstname" value="$contactFirstName" /></td></tr>
            <tr><td>Contact Last Name</td><td><input type="text" name="contactlastname" value="$contactLastName" /></td></tr>
            <tr><td>Address 1</td><td><input type="text" name="address1" value="$address1" /></td></tr>
            <tr><td>Address 2</td><td><input type="text" name="address2" value="$address2" /></td></tr>
            <tr><td>Suburb</td><td><input type="text" name="suburb" value="$suburb" /></td></tr>
            <tr>
                <td>City/Town</td>
                <td>
                    <select name="citytownid">
                        $cityTownOptions
                    </select>
                </td>
            </tr>
            <tr><td>Region</td>
                <td>
                    <select name="regionid">
                        $regionOptions
                    </select>
                </td>
            </tr>
            <tr>
                <td>Country</td>
                <td>
                    <select name="countryid">
                        $countryOptions
                    </select>
                 </td>
             </tr>
            <tr><td>Telephone</td><td><input type="text" name="telephone" value="$telephone" /></td></tr>
            <tr><td>Telephone DD</td><td><input type="text" name="telephonedd" value="$telephoneDD" /></td></tr>
            <tr><td>Fax</td><td><input type="text" name="fax" value="$fax" /></td></tr>
            <tr><td>Blurb</td><td><input type="textarea" name="blurb" rows="10" style="height:200px;" value="$blurb" /></td></tr>
            <tr><td><input type="submit" value="Update" /></td></tr>
        </table>
    </form>
</body>
</html>

EOT;
}
?>
